#define _GNU_SOURCE
#include "pthread.h"
#include "unistd.h"
#include "stdio.h"

void *thread_func1(void *arg)
{
    int i = 0;
    while (i < 10)
    {
        printf("thread1 is running ,tid=%d\n", gettid());
        sleep(1);
        i++;
    }
    printf("thread1 is exiting\n");
    return NULL;
}
void *thread_func2(void *arg)
{
    int i = 0;
    while (i < 5)
    {
        printf("thread2 is running ,tid=%d\n", gettid());
        sleep(1);
        i++;
    }
    printf("thread2 is exiting\n");
    return NULL;
}
int main(int argc, char const *argv[])
{
    // 声明线程句柄
    pthread_t thread1, thread2;
    // 创建线程
    pthread_create(&thread1, NULL, thread_func1, NULL);
    pthread_create(&thread2, NULL, thread_func2, NULL);

    // 此时进程中一共有3个线程，主线程，thread1，thread2
    sleep(10);
    // pthread_cancel： 取消线程thread1
    //  pthread_cancel(thread1);
    //  等待子线程结束
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    printf("main thread is exiting\n");

    return 0;
}
